//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Xml;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for FullyQualifiedUrlModule.
	/// </summary>
	public class FullyQualifiedUrlModule : ICSModule
	{
		#region ICSModule Members

        private bool _runtime = false;

        public void Init(CSApplication csa, XmlNode node)
        {

            if(node != null)
            {
                XmlAttribute att = node.Attributes["runtime"];
                if(att != null && att.Value == "true")
                    _runtime = true;
            }

            if(_runtime)
                csa.PreRenderPost +=new CSPostEventHandler(csa_PreRenderPost);
            else
                csa.PrePostUpdate +=new CSPostEventHandler(csa_PrePostUpdate);
        }

        #endregion

        private void csa_PreRenderPost(IContent content, CSPostEventArgs e)
        {
            if(e.Target == PostTarget.Syndication || e.Target == PostTarget.Nntp || e.Target == PostTarget.MailGateway)
            {
                string hostPath = CSContext.Current.HostPath;
                if(!Globals.IsNullorEmpty(hostPath))
                {
                    Post p = content as Post;
                    if(p != null)
                        p.FormattedBody = Formatter.ConvertLocalUrls(p.FormattedBody, hostPath);
                }            
            }
        }

        private void csa_PrePostUpdate(IContent content, CSPostEventArgs e)
        {
            string hostPath = CSContext.Current.HostPath;
            if(!Globals.IsNullorEmpty(hostPath))
            {
                Post p = content as Post;
                if(p != null)
                    p.FormattedBody = Formatter.ConvertLocalUrls(p.FormattedBody, hostPath);
               
            }
            
        }
    }
}
